package edu.netjava.ClientChater;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientChater {

	String destHost;
	int destPort;

	public ClientChater(String destHost, int port) {
		this.destHost = destHost;
		this.destPort = port;

	}

	public void getConnection() {

		try {
			Socket soc = new Socket(destHost, destPort);

			if (soc.isConnected()) {
				InputStream is = soc.getInputStream();
				OutputStream os = soc.getOutputStream();

				InputStreamReader isr = new InputStreamReader(is, "GBK");
				BufferedReader dis = new BufferedReader(isr);
				OutputStreamWriter ops = new OutputStreamWriter(os, "GBK");
				BufferedWriter bfw = new BufferedWriter(ops);

				BufferedReader br = new BufferedReader(new InputStreamReader(
						System.in));
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						System.out));
				String inputLine = null;
				while (((inputLine = br.readLine()) != null)
						&& (!inputLine.equals("bye"))) {
					bw.write(inputLine + "\r\n");
					bfw.write(inputLine + "\r\n");
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		
		ClientChater cc = new ClientChater("localhost",9999);
		cc.getConnection();

	}

}
